/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmail.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author iflamed
 */
public class Endir extends File {

    private File dirctory;
    private File filename;

    /**
     * Constructor extends File
     *
     * @param dir the path of the file without filename
     * @param file the filename
     */
    public Endir(String dir, String file) {
        super(dir + file);
        this.dirctory = new File(this.getParent());
        this.filename = this.getAbsoluteFile();
    }

    /**
     * Constructor extends File
     *
     * @param filePath the path of the file with the file name
     */
    public Endir(String filePath) {
        super(filePath);
        if (!this.exists()) {
            this.dirctory = new File(this.getParent());
            this.filename = this.getAbsoluteFile();
        } else {
            this.dirctory = this.getParentFile();
            this.filename = this.getAbsoluteFile();
        }
    }

    /**
     *
     * @param stuName student name
     * @param stuNumber student number
     * @return File a
     * <code>File</code>
     * @throws IOException
     * @throws JDOMException
     */
    public File createFile(String stuName, String stuNumber) throws IOException, JDOMException {
        if (dirctory.exists()) {
            if (!filename.exists()) {
                filename.createNewFile();
                createXml(stuName, stuNumber);
            }
            System.out.println("文件创建成功:" + filename.getAbsolutePath());
        } else {
            dirctory.mkdirs();
            filename.createNewFile();
            createXml(stuName, stuNumber);
            System.out.println("文件创建成功：" + filename.getAbsolutePath());
        }
        return filename;
    }

    /**
     * create a file which filename initialed by constructor
     *
     * @return File
     * @throws IOException
     */
    public File createTempFile() throws IOException {
        if (dirctory.exists()) {
            filename.createNewFile();
            System.out.println("文件创建成功:" + filename.getAbsolutePath());
        } else {
            dirctory.mkdirs();
            filename.createNewFile();
            System.out.println("文件创建成功：" + filename.getAbsolutePath());
        }
        return filename;
    }

    public boolean writeFile(String detail) throws IOException {
        FileWriter filewriter = new FileWriter(filename, true); //false表示不追加内容  
        if (filewriter != null) {
            filewriter.write(detail);
            filewriter.close();
            return true;
        }
        return false;
    }

    private void createXml(String name, String number) throws IOException, JDOMException {
        Element root, stuNumber, stuName, assignments, totalScore;
        root = new Element("student-info");
        stuNumber = new Element("student-number");
        stuName = new Element("student-name");
        assignments = new Element("assignments");
        totalScore = new Element("totalscore");
//        assignment = new Element("assignment");
//        score = new Element("score");
//        attachs = new Element("attachs");
//        attach = new Element("attach");
//        fileName = new Element("filename");
//        filePath = new Element("filepath");
        stuNumber.setText(number);
        stuName.setText(name);
        Document doc = new Document();
        doc.setRootElement(root);
        totalScore.setText("0");
        root.addContent(totalScore);
        root.addContent(stuNumber);
        root.addContent(stuName);
        root.addContent(assignments);
        Format format = Format.getCompactFormat();
        format.setEncoding("GB2312"); //设置xml文件的字符为gb2312
        format.setIndent("    "); //设置xml文件的缩进为4个空格
        XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格
        XMLOut.output(doc, new FileOutputStream(filename));
    }
}
